#!/usr/bin/env python
# -*- coding: utf-8 -*-

import random
from collections import namedtuple
import string

User = namedtuple('User', 'uid name')

def create_user(uid, name):
    return User(uid, name)

def create_test_users():
    return [
        create_user(0,'Per'),
        create_user(1,'Kari'),
        create_user(2,'Terje')
    ]

def generate_unique_game_id():
    return random.randint(0,100000000)

def generate_game_letters(nr_letters=8):
    """
    Generate a list with random lowercase letters.
    """
    chars = string.ascii_lowercase
    return [random.choice(chars) for x in range(nr_letters)]


class RandomGameLetters(object):
    """

    """
    count = 'count'

    def __init__(self, letters):
        """
        letters will usually be the dictionary at the end of this file. We have 
        it as an input argument only to allow other flexible ways of using it.
        """
        self.letters = letters

    def get_all_letters(self):
        """
        Return the entire set of valid letters for this user and game
        """
        return self.letters['characters']

    def get_base_object(self):
        return self.letters

    def get_active_letters(self):
        """
        Return the list of 'active' valid characters. If none found (typical for 
        first run) we will generate them.
        """
        if not self.letters.get('active_letters'):
            self.letters['active_letters'] = self.get_rand_letters()
        return self.letters.get('active_letters')

    def get_rand_letters(self, nr_of_letters=8):
        """
        Generate a string of letters based on the "allowed" chars taken from our 
        'self.letters' dictionary.

        Note that foreach character "taken" out, the internal counters will be 
        updated.
        """
        chars = []
        filtered_letters_keys = [x for x in self.letters['characters'].keys() if self.letters['characters'][x][self.count] > 0]
        for i in xrange(nr_of_letters):
            char = random.choice(filtered_letters_keys)
            chars.append(char)

            # decrease the count score
            self.letters['characters'][char][self.count] -= 1
        return chars

    def is_letter_valid(self, w):
        o = self.letters['characters'].get(w)
        if o.get(self.count) > 0:
            return True
        return False

    def is_all_letters_valid(self, word):
        for w in word:
            if not self.is_letter_valid(w):
                return False

        return True


def create_rand_letters_object():
    """
    This is the main entry point to get a 'fresh' new RandomGameLetters object
    """
    return RandomGameLetters(letters.copy())


letters = {'active_letters':[],
        'characters': {
            u'a':{'count':7, 'points':1},
            u'b':{'count': 3, 'points': 4},
            u'c':{'count': 1, 'points': 10},
            u'd':{'count': 5, 'points': 1},
            u'e':{'count': 9, 'points': 1},
            u'f':{'count': 4, 'points': 2},
            u'g':{'count': 4, 'points': 4},
            u'h':{'count': 3, 'points': 3},
            u'i':{'count': 6, 'points': 2},
            u'j':{'count': 2, 'points': 4},
            u'k':{'count': 4, 'points': 3},
            u'l':{'count': 5, 'points': 2},
            u'm':{'count': 3, 'points': 2},
            u'n':{'count': 6, 'points': 1},
            u'o':{'count': 4, 'points': 3},
            u'p':{'count': 2, 'points': 4},
            u'r':{'count': 7, 'points': 1},
            u's':{'count': 7, 'points': 1},
            u't':{'count': 7, 'points': 1},
            u'u':{'count': 3, 'points': 4},
            u'v':{'count': 3, 'points': 5},
            u'w':{'count': 1, 'points': 10},
            u'y':{'count': 1, 'points': 8},
            u'æ':{'count': 1, 'points': 8},
            u'ø':{'count': 2, 'points': 4},
            u'å':{'count': 2, 'points': 4},
            }
        }

